VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsFXRSParser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'************************************************************************************
'Name       : clsFXRSParser
'Author     : Chris Elwell
'Date       : 09/18/03
'Modified   : 08/16/04
'Changes    : Had to modify ParseData to look for CRLF instead of LF in Eltron label
'             buffer.
'Description: This is a class module that can be used in your projects that are
'   doing transactions with FXRS. Passing a reply transaction to the ParseData
'   method results in all the fields and data being parsed into arrays called
'   Field(Index) and Data(Index). Use the ParseComplete event to trigger your program
'   to grab the fields you require.
'
'FXRSParser class details:
'   Read Properties:
'       Field(Index) As String - stores field numbers
'       Data(Index) As String - stores data
'       TotalFields As Integer - total fields in the reply transaction
'       Parse_Error As String
'   Write Properties:
'       None
'   Events:
'       ParseComplete
'       ParseError
'   Methods:
'       ParseData(Data As String)
'
'Usage:
'1. Register clsFXRSParser.dll and then in your project select FXRSParser under References.
'2. Declare a class object variable using WithEvents
'3. Pass FXRS reply string to ParseData method. The class verifies the data
'   string begins with a zero character and ends with 99,"" characters.
'4. Create a ParseComplete event sub for the object.
'5. Create a For/Next loop in the ParseComplete event sub.
'   Set the range = 0 to .TotalFields - 1
'6. Inside the For/Next loop create an If/Then or Select Case statement to
'   grab the .Data from the required .Field numbers.
'   E.G.
'   With clsFXRS
'       For i = 0 to .TotalFields - 1
'           Select Case .Field(i)
'               Case "29"
'                   strTrackingNumber = .Data(i)
'               Case "37"
'                   dblNetCharges = Val(.Data(i)) * .01
'               Case "60"
'                   intBilledWeight = Val(.Data(i))
'           End Select
'       Next
'   End With
'7. Create a ParseError event sub and display the Parse_Error property in the
'   event of an error.
'****************************************************************************
Option Explicit

Private m_Field(250) As String
Private m_Data(250) As String
Private m_TotalFields As Integer
Private m_ParseError As String

Public Event ParseComplete()
Public Event ParseError()

Public Property Get Field(intIndex As Integer) As String
    Field = m_Field(intIndex)
End Property

Public Property Get Data(intIndex As Integer) As String
    Data = m_Data(intIndex)
End Property

Public Property Get TotalFields() As Integer
    TotalFields = m_TotalFields
End Property

Public Property Get Parse_Error() As String
    Parse_Error = m_ParseError
End Property

Public Sub ParseData(strReply As String)
    
    On Local Error GoTo ERROR_HANDLER

    Dim intPosition As Integer      'Counter for position in strReply
    Dim intIndex As Integer         'Index for m_Field and m_Data vars
    
    'Clear the arrays and initialize variables
    Dim i
    For i = 0 To 249
        m_Field(i) = ""
        m_Data(i) = ""
    Next i
    m_ParseError = ""
    m_TotalFields = 0
    intPosition = 1
    intIndex = 0
    
    'Check to ensure that the string begins with 0 and 99,"" ends the string
    If Left(strReply, 1) <> "0" Or InStr(1, strReply, "99," & Chr(34) & Chr(34)) = 0 Then
        m_ParseError = "Invalid FXRS reply string"
        RaiseEvent ParseError
        Exit Sub
    End If
      
    'Main loop, runs until 99,"" is reached
    Do While Mid(strReply, intPosition, 5) <> ("99," + Chr(34) + Chr(34))
        'Parse the field number
        Do While Mid(strReply, intPosition, 1) <> ","
            m_Field(intIndex) = m_Field(intIndex) + Mid(strReply, intPosition, 1)
            intPosition = intPosition + 1
        Loop
        'Increment past the first double-quote
        intPosition = intPosition + 2
        
        'Parse the data
        'Eltron label buffer contains double quotes so look for end of buffer
        'Modified 8/16 to look for CRLF + P1 + CRLF as the end of the buffer
        If (m_Field(intIndex) = "188" Or m_Field(intIndex) = "411") And InStr(1, strReply, Chr(13) + Chr(10) + "P1" + Chr(13) + Chr(10)) > 0 Then
            Do While Mid(strReply, intPosition - 6, 6) <> Chr(13) + Chr(10) + "P1" + Chr(13) + Chr(10)
                
                    m_Data(intIndex) = m_Data(intIndex) + Mid(strReply, intPosition, 1)
                    intPosition = intPosition + 1
            Loop
        'Else grab one character at a time until reaching the second quote
        Else
            Do Until Mid(strReply, intPosition, 1) = Chr(34)
                m_Data(intIndex) = m_Data(intIndex) + Mid(strReply, intPosition, 1)
                intPosition = intPosition + 1
            Loop
        End If
        'Increment position past 2nd double-quote and increment Field/Data index
        intPosition = intPosition + 1
        intIndex = intIndex + 1
    Loop
    m_TotalFields = intIndex
    RaiseEvent ParseComplete
    Exit Sub
    
ERROR_HANDLER:
    m_ParseError = Err.Description
    RaiseEvent ParseError

End Sub
